#include <iostream>

class Person {
public:
    int age;
    int *height;
public:
    Person() {
        std::cout << "无参数构造" << std::endl;
    }

    Person(int age, int height) : age(age)  {
        this->height = new int(height);
        std::cout << "有参构造" << std::endl;
    }

    // 自己实现拷贝构造函数 解决浅拷贝带来的问题
    Person(const Person &p) {
        age = p.age;
//        height = p.height; // 编译器默认实现就是这行代码

        // 神拷贝
        height = new int(*p.height);
    }

    virtual ~Person() {
        if (height != NULL) {
            delete height;
            height = NULL;
        }
        std::cout << "析构函数" << std::endl;
    }
};

void test01() {
    Person p1(18, 160);

    std::cout << "p1的年龄" << p1.age << "身高"  << *p1.height << std::endl;

    Person p2(p1);
    std::cout << "p2的年龄" << p2.age << "身高"  << *p2.height << std::endl;


}

int main() {

    test01();
    return 0;
}

